#define _CRT_SECURE_NO_WARNINGS

#include "project.h"


void SLTPrint(SLTNode* phead)
{
	assert(phead);
	SLTNode* pur = phead;
	while (pur)
	{
		printf("%s ", pur->data->name);
		printf("%s ", pur->data->sex);
		printf("%d ", pur->data->age);
		printf("%s ", pur->data->tel);
		printf("%s ", pur->data->addr);
		printf("\n");
		pur = pur->next;
	}
}


SLTNode* SLTBuyNode(SLTDataType x)
{
	SLTNode* newcode = (SLTNode*)malloc(sizeof(SLTNode));
	if (newcode == NULL)
	{
		perror("malloc fail");
		exit(1);
	}
	*newcode->data = x;
	newcode->next = NULL;
	return newcode;
}

void SLTPushBack(SLTNode** pphead, SLTDataType x)
{
	assert(pphead);
	SLTNode* new = SLTBuyNode(x);
	if (*pphead == NULL)
	{
		*pphead = new;
	}
	else
	{
		SLTNode* pur = *pphead;
		while (pur->next)
		{
			pur = pur->next;
		}
		pur->next = new;
	}
}

void SLTPushFront(SLTNode** pphead, SLTDataType x)
{
	assert(pphead);
	SLTNode* new = SLTBuyNode(x);
	new->next = *pphead;
	*pphead = new;
}

void SLTPopBack(SLTNode** pphead)
{
	assert(pphead && *pphead);
	SLTNode* pur = *pphead;
	if ((*pphead)->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else
	{
		SLTNode* prev = *pphead;
		SLTNode* ptail = *pphead;
		while (ptail->next)
		{
			prev = ptail;
			ptail = ptail->next;
		}
		prev->next = NULL;
		ptail = NULL;
		free(ptail);
	}
}

void SLTPopFront(SLTNode** pphead)
{
	assert(pphead && *pphead);
	SLTNode* node = (*pphead)->next;
	(*pphead)->next = NULL;
	free(*pphead);
	*pphead = node;
}

//SLTNode* SLTFind(SLTNode* phead, SLTDataType x)
//{
//	SLTNode* pur = phead;
//	while (pur)
//	{
//		if (pur->data == x)
//		{
//			return pur;
//		}
//		pur = pur->next;
//	}
//	return NULL;
//}


void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)
{
	assert(pphead && *pphead);
	assert(pos);
	SLTNode* pur = *pphead;
	SLTNode* new = SLTBuyNode(x);
	if (*pphead == pos)
	{
		SLTPushFront(pphead, x);
	}
	else
	{
		while (pur->next != pos)
		{
			pur = pur->next;
		}
		new->next = pos;
		pur->next = new;
		
	}
}

void SLTErase(SLTNode** pphead, SLTNode* pos)
{
	assert(*pphead);
	if (*pphead == pos)
	{
		SLTNode* prev = *pphead;
		*pphead = (*pphead)->next;
		free(prev);
	}
	else
	{
		SLTNode* pur = *pphead;
		while (pur->next != pos)
		{
			pur = pur->next;
		}
		SLTNode* pucr = pos->next;
		pur->next = pucr;
		pos->next = NULL;
		free(pos);
	}
}

void SLTInsertAfter(SLTNode* pos, SLTDataType x)
{
	SLTNode* new = SLTBuyNode(x);
	SLTNode* pur = pos->next;
	pos->next = new;
	new->next = pur;
}

void SLTEraseAfter(SLTNode* pos)
{
	SLTNode* pur = pos->next;
	SLTNode* pucr = pur->next;
	pos->next = pucr;
	pur->next = NULL;
	free(pur);
}

void SListDesTroy(SLTNode** pphead)
{
	SLTNode* pur = *pphead;
	while ((*pphead)->next != NULL)
	{
		pur = *pphead;
		*pphead = (*pphead)->next;
		pur->next = NULL;
		free(pur);
	}
	free(*pphead);
}

